#!/bin/bash
#set -e

#change source
sudo sh -c "echo '
deb http://repo.huaweicloud.com/ubuntu/ focal main restricted universe multiverse
deb-src http://repo.huaweicloud.com/ubuntu/ focal main restricted universe multiverse
deb http://repo.huaweicloud.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://repo.huaweicloud.com/ubuntu/ focal-security main restricted universe multiverse
deb http://repo.huaweicloud.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://repo.huaweicloud.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://repo.huaweicloud.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://repo.huaweicloud.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://repo.huaweicloud.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://repo.huaweicloud.com/ubuntu/ focal-backports main restricted universe multiverse
' > /etc/apt/sources.list"

#updata
sudo sh -c 'apt update -y;apt upgrade -y;apt autoremove -y'

#install chinese pack
sudo apt install -y language-pack-zh-hans language-pack-zh-hans-base
sudo sed -i '$aLANG=zh_CN.UTF-8' ~/.profile
sudo apt-get install manpages-dev
sudo apt-get install manpages-zh

#install software
git config --global user.name "风吹花中花吹风"
git config --global user.email "791314247@qq.com"
git config --global credential.helper store
sudo apt install git-lfs
sudo apt install scons -y
sudo apt-get install -y build-essential gcc g++ make zlib* libffi-dev e2fsprogs pkg-config flex bison perl bc openssl libssl-dev libelf-dev binutils binutils-dev libdwarf-dev u-boot-tools mtd-utils

#设置python和python3软链接为python3.8
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1

#install python packet
sudo apt-get install python3-setuptools python3-pip -y
sudo pip3 install --upgrade pip
pip3 install -i https://mirrors.aliyun.com/repository/pypi/simple requests
pip3 install --trusted-host mirrors.aliyun.com -i https://mirrors.aliyun.com/pypi/simple setuptools
pip3 install --trusted-host mirrors.aliyun.com -i https://mirrors.aliyun.com/pypi/simple kconfiglib
pip3 install --trusted-host mirrors.aliyun.com -i http://mirrors.aliyun.com/pypi/simple pycryptodome
pip3 install --trusted-host mirrors.aliyun.com -i https://mirrors.aliyun.com/pypi/simple ecdsa

#install software
cd ~/harmony/tool || exit
sudo chmod a+x ./*
sudo cp ./repo /usr/local/bin/

#gn
sudo rm -rf ~/gn
mkdir ~/gn
tar -xvf gn-linux-x86-1717.tar.gz -C ~/gn
grep -q "export PATH=~/gn:\$PATH" ~/.bashrc || \
sudo sed -i '$aexport PATH=~/gn:$PATH' ~/.bashrc

#ninja
sudo rm -rf ~/ninja
mkdir ~/ninja
tar -xvf ninja.1.9.0.tar -C ~/
grep -q "export PATH=~/ninja:\$PATH" ~/.bashrc || \
sudo sed -i '$aexport PATH=~/ninja:$PATH' ~/.bashrc

#llvm
sudo rm -rf ~/llvm
mkdir ~/llvm
find ./ -name llvm-linux-9.0.0-36191.tar | grep -q llvm-linux-9.0.0-36191.tar || wget -P ./ https://repo.huaweicloud.com/harmonyos/compiler/clang/9.0.0-36191/linux/llvm-linux-9.0.0-36191.tar
tar -xvf llvm-linux-9.0.0-36191.tar -C ~/
grep -q "export PATH=~/llvm/bin:\$PATH" ~/.bashrc || \
sudo sed -i '$aexport PATH=~/llvm/bin:$PATH' ~/.bashrc

#gcc_riscv32
sudo rm -rf ~/gcc_riscv32
mkdir ~/gcc_riscv32
tar -xvf gcc_riscv32-linux-7.3.0.tar.gz -C ~/
grep -q "export PATH=~/gcc_riscv32/bin:\$PATH" ~/.bashrc || \
sudo sed -i '$aexport PATH=~/gcc_riscv32/bin:$PATH' ~/.bashrc

#hb
python3 -m pip install --user ohos-build
grep -q "export PATH=~/.local/bin:\$PATH" ~/.bashrc || \
sudo sed -i '$aexport PATH=~/.local/bin:$PATH' ~/.bashrc

#update source
cd ~/
source .bashrc

#get code
find ~/harmony/ -name code | grep -q code || mkdir ~/harmony/code
cd ~/harmony/code || exit
repo init -u https://gitee.com/openharmony/manifest.git -b refs/tags/OpenHarmony-v1.1.1-LTS --no-repo-verify
repo sync -c && repo forall -c 'git lfs pull'

#build config
hb set
cd ~/harmony/code
hb build
